package cn.bocweb.spinning.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.LibListBean;
import cn.bocweb.spinning.model.Bean.MyClassBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.adapter.MySchoolAdapter;
import cn.bocweb.spinning.view.common.BaseActivity;
import cn.bocweb.spinning.view.common.GlideCircle;
import cn.bocweb.spinning.view.common.MagnificentChart;
import cn.bocweb.spinning.view.common.MagnificentChartItem;
import cn.bocweb.spinning.view.common.SwipeListView;
import de.greenrobot.event.EventBus;

public class MySchoolActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.my_icon)
    ImageView myIcon;
    @Bind(R.id.my_name)
    TextView myName;
    @Bind(R.id.icon_gender)
    ImageView iconGender;
    @Bind(R.id.my_username)
    TextView myUsername;
    @Bind(R.id.my_realname)
    TextView myRealname;
    @Bind(R.id.count_right)
    TextView countRight;
    @Bind(R.id.count_wrong)
    TextView countWrong;
    @Bind(R.id.count_miss)
    TextView countMiss;
    @Bind(R.id.btn_process_detail)
    RelativeLayout btnProcessDetail;
    @Bind(R.id.round_process)
    MagnificentChart roundProcess;
    MyClassBean.ProgressEntity progressEntity;

    List<MagnificentChartItem> magnificentChartItems;
    UserPresenter presenter;
    String userid, libid, libString;
    @Bind(R.id.question_title)
    TextView questionTitle;
    @Bind(R.id.my_school_list)
    SwipeListView mySchoolList;
    MySchoolAdapter mySchoolAdapter;
    List<MyClassBean.RecordEntity> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_school);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        initToolbar();
        initClickListener();
        initList();
        initModel();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

        DeviceUtil.Log("list 2 = " + list.size());
    }

    private void initList() {
        list = new ArrayList<>();
        mySchoolAdapter = new MySchoolAdapter(this, list);
        mySchoolList.setAdapter(mySchoolAdapter);
    }

    @Override
    protected void onPause() {
        EventBus.getDefault().unregister(this);
        super.onPause();
    }

    @Override
    protected void onStop() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onStop();
    }

    private void initModel() {
        UserBean.DataEntity user = new UserBean.DataEntity();
        user.readInfoFromLocal(this);
        userid = user.getUserid();

        Glide.with(this).load(user.getUthumb()).transform(new GlideCircle(this)).into(myIcon);
        myName.setText(user.getNickname());
        if (user.getGender().equals("1")) {
            iconGender.setImageResource(R.mipmap.rank_male);
        } else {
            iconGender.setImageResource(R.mipmap.rank_female);
        }
        myUsername.setText(user.getPhone());
        myRealname.setText(user.getRealname());

        LibListBean.DataEntity lib = new LibListBean.DataEntity();
        lib.readLibListIntoLocal(this);
        libid = lib.getId();
        libString = lib.getTitle();
        questionTitle.setText(libString);

        presenter = new UserPresenterImp();
        presenter.myClass(userid, libid);
        showLoading();
    }

    /**
     * 获得我的学堂数据
     * @param bean
     */
    public void onEventMainThread(MyClassBean bean) {
        hideLoading();
        if (bean.getReturn_code() == 0) {
            list.addAll(bean.getRecord());
            mySchoolAdapter.notifyDataSetChanged();

            progressEntity = bean.getProgress();

            countRight.setText(String.valueOf(bean.getProgress().getRight()));
            countWrong.setText(String.valueOf(bean.getProgress().getWrong()));
            countMiss.setText(String.valueOf(bean.getProgress().getBlank()));

            initMagnificentChart(bean);
        } else if (bean.getReturn_code() == -204) {
            //这里是考试纪录为空的情况下
            countRight.setText(String.valueOf(bean.getProgress().getRight()));
            countWrong.setText(String.valueOf(bean.getProgress().getWrong()));
            countMiss.setText(String.valueOf(bean.getProgress().getBlank()));

            initMagnificentChart(bean);
        } else {
            DeviceUtil.MyToast(this, bean.getMsg());
        }

    }

    private void initMagnificentChart(MyClassBean bean) {
        magnificentChartItems = new ArrayList<>();

        DecimalFormat df0 = new DecimalFormat("######0");
        DecimalFormat df1 = new DecimalFormat("######0.0");
        DecimalFormat df2 = new DecimalFormat("######0.00");

        double total = bean.getProgress().getTotal();
        double right = bean.getProgress().getRight() * 100 / total;
        double wrong = bean.getProgress().getWrong() * 100 / total;
        double miss = 100 - right - wrong;

        roundProcess.setText(df1.format(right + wrong) + "%");

        DeviceUtil.Log("right = " + right + " wrong = " + wrong + " miss = " + miss);

        MagnificentChartItem item = new MagnificentChartItem("做对", right , getResources().getColor(R.color.green));
        MagnificentChartItem item1 = new MagnificentChartItem("做错", wrong , getResources().getColor(R.color.red));
        MagnificentChartItem item2 = new MagnificentChartItem("未做", miss, getResources().getColor(R.color.yellow));
        magnificentChartItems.add(item);
        magnificentChartItems.add(item1);
        magnificentChartItems.add(item2);

        roundProcess.setChartItemsList(magnificentChartItems);
        roundProcess.setMaxValue(100);
    }

    private void initClickListener() {
        btnProcessDetail.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_process_detail: {
                intent = new Intent(this, CountActivity.class);
                intent.putExtra(Constant.COUNT_ACITIVYT, progressEntity);
                DeviceUtil.Log("list = " + list.size());
                startActivity(intent);
                break;
            }
            case R.id.btn_score_detail: {
                intent = new Intent(this, TestResultActivity.class);
                startActivity(intent);
                break;
            }
        }
    }

    private void initToolbar() {
        toolbar.setTitle("我的学堂");
        toolbar.setTitleTextColor(getResources().getColor(R.color.title));
        toolbar.setNavigationIcon(R.mipmap.info_detail_exit);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


}
